<template>
  <div class="container">
    <div class="app-container">
      <!-- 放置树形组件 -->
      <el-tree :data="depts" :props="defaultProps" :expand-on-click-node="false">
        <!-- 布局组件 el-row-el-col -->
        <template v-slot="{ data }">
          <el-row style="width: 100%; height: 40px; " type="flex" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col>
              <el-row type="flex" justify="end">
                <span class="username">管理员</span>
                <el-dropdown @command="operateDept($event, data.id)">
                  <!-- 显示区域内容 -->
                  <span class="el-dropdown-link">
                    操作<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <!-- 下拉菜单选项 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                    <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                    <el-dropdown-item command="del">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-row>
            </el-col>
          </el-row>
        </template>

      </el-tree>
    </div>
    <!-- 弹层 -->
    <!-- ref获取所有的实例方法 -->
    <add-dept ref="addDept" v-loading="loading" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" class="container" @updateDept="getDepartment" />

  </div>

</template>

<script>
import { getDepartMentAPI } from '@/api'
import { transListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
import { delDepartMentAPI } from '@/api/department.js'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      depts: [{
        name: '传智教育',
        children: [
          { name: '总裁办' },
          { name: '行政部' },
          { name: '财务部' }
        ]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      currentNodeId: null // 存储当前点击的id
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 封装好方法
    async getDepartment() {
      this.loading = true
      this.depts = transListToTreeData(await getDepartMentAPI(), 0)
      this.loading = false
    },
    // 操作部门方法
    // 点击下拉菜单的方法
    async operateDept(type, id) {
      if (type === 'add') {
        this.showDialog = true // 显示弹层
        this.currentNodeId = id
      } else if (type === 'edit') {
        // 编辑场景
        // Props的渲染是异步的
        this.currentNodeId = id // 给data赋值 传递给props
        // nextTick => 微任务 异步 nextTick会等到组件的上一次更新完成之后执行
        // this.$nextTick(() => {
        //   // 回调函数会等到上一次更新完成之后
        //   this.$refs.addDept.getDepartMentDetail()
        // })
        await this.$refs.addDept.getDepartMentDetail(id)
        this.showDialog = true // 编辑场景下显示弹层
      } else {
        // 删除场景
        await this.$confirm('确定删除该部门吗')
        // 调用删除接口
        await delDepartMentAPI(id)
        this.$message.success('删除部门成功')
        this.getDepartment() // 重新拉取数据
      }
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px 50px;
}
.username {
  margin-right: 10px;
  font-size: 14px;
}
</style>
